local skel = fk.CreateSkill{
  name = "tea__fenyuan",
  tags = { Skill.Hidden }
}
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__fenyuan"] = "焚缘",
  [":tea__fenyuan"] = "隐匿技，你登场的回合结束时，你可以将一张手牌当【出其不意】使用，若以此法使用或展示的牌为♠，你可以重复此流程。",
  ["#tea__fenyuan-invoke"] = "焚缘：你可以将一张手牌当【出其不意】使用",
  ["@@tea__fenyuan-turn"] = "焚缘"
}
---@type TrigSkelSpec<TrigFunc>
local fenyuan_recorder = {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player == target
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@tea__fenyuan-turn", 1)
    player:broadcastSkillInvoke(skel.name, 1)
    room:doAnimate("InvokeSkill", {
      name = skel.name,
      player = player.id,
      skill_type = "special",
    })
  end
}
---@type TrigSkelSpec<TurnFunc>
local fenyuan = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasShownSkill(skel.name) and player:getMark("@@tea__fenyuan-turn") ~= 0 and not player:isKongcheng()
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(skel.name, table.random{2, 3})
    room:notifySkillInvoked(player, skel.name, "offensive")
    local suit1, suit2 = Card.Spade, Card.Spade
    while not player.dead or not player:isKongcheng() do
      local suc, dat = room:askToUseActiveSkill(player, {
        skill_name = "tea__fenyuan_viewas",
        prompt = "#tea__fenyuan-invoke",
        skip = true
      })
      if suc and dat then
        local card = Fk:cloneCard("unexpectation")
        card:addSubcards(dat.cards or {})
        card.skillName = skel.name
        ---@diagnostic disable-next-line: assign-type-mismatch
        card.skill = Fk.skills["tea__fenyuan__unexpectation_skill"]

        local use = { ---@type UseCardDataSpec
          from = player,
          tos = dat.targets,
          card = card,
        }
        room:useCard(use)

        suit1 = card.suit
        suit2 = Card.NoSuit
        local end_id = room.logic:getCurrentEvent():findParent(GameEvent.Turn).id
        room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
          if e.data.card == card and e.data.extra_data and e.data.extra_data.unexpectation_shown_card then
            suit2 = e.data.extra_data.unexpectation_shown_card.suit ---@type integer
            return true
          end
        end, end_id or 1)
        if suit1 ~= Card.Spade and suit2 ~= Card.Spade then break end
      else
        break
      end
    end
  end
}
skel:addEffect(fk.TurnEnd, fenyuan)
skel:addEffect(U.GeneralAppeared, fenyuan_recorder)
-- Fk.generals["mouxusheng"]:addSkill(skel.name)
return skel